var selectgrid,gridGoodsDiv_table;
var shelfGoodslist;
var data = {};
function handleStore(){
	$('#data_div').css({"height":($("#win_function").height()/2-1)+"px"});
	if(objs.length==0){
		sys_alert('错误提示', '至少选择一个物品入库', 'error');
	}else{
		greyLayer.open();
		win_func.window('open');
		form_func.form('clear');
		
		if($('#room_id').next().children().length!=0){
			$('#room_id').show();
			$('#room_id').next().children()[0].style.width=($('#room_id').width())+'px';
			$('#room_id')[0].style.display='none';
		}
		$('#gridDiv').hide();
		$('#room_id').combobox({
			url:proName+"/casePropertyStorage/getCaseRoom",
			multiple:false,
			onChange:function(newValue,oldValue){  
				$('#gridGoodsDiv').hide();
				// $('#shelf_id').show().next().remove();
				if($('#shelf_id').next().children().length!=0){
					$('#shelf_id').show();
					$('#shelf_id').next().children()[0].style.width=($('#shelf_id').width())+'px';
					$('#shelf_id')[0].style.display='none';
				}
				
				$('#shelf_id').combobox({
					url:proName+"/casePropertyStorage/getShelfByCaseRoom?room_id="+newValue,
					multiple:false,
					onChange:function(newValue,oldValue){
						$('#gridGoodsDiv').hide();
						if(newValue==""){
							return;
						}
						$.post(proName+"/casePropertyStorage/getCabinet?shelf_id="+newValue,function(result){
							var gridDiv = $('#gridDiv');
							gridDiv.empty();
							if(result.error){
								shelfGoodslist = [];
								data = {};
								sys_alert('错误提示', result.error, 'error');
								return;
							}
							$('#gridDiv').show();
							data = {};
							shelfGoodslist = result.shelfGoodslist;
							var div_hight = $("#win_function").height()/2;
							var col_Num = result.col_Num;
							var row_Num = result.row_Num;
							var table = $('<table>');
							var div_hight = ($("#win_function").height()/2-40)/row_Num;
							table.css({"width":"100%","border": "solid 1px #000000"});
							for(var i=0;i<row_Num;i++){
								var tr = $('<tr>');
								for(var j=0;j<col_Num;j++){
									var td = $('<td>');
									var div = $('<div>');
									div.css({"height":div_hight+"px","line-height":div_hight+"px","text-align":"center"});
									var displayName = result.gridInfoList[i*col_Num+j].grid_num;
									var div_id = result.gridInfoList[i*col_Num+j].grid_id;
									
									var div_color = result.gridInfoList[i*col_Num+j].grid_status;
									div.attr('id',div_id);
									if(div_color=="1"){
										displayName += '(存有物品)';
										data[div_id] = new Array();
										div.attr("isUsing","1");
										div.css({"background-color": "pink","cursor":"pointer"});
									}else{
										displayName += '(未存物品)';
										div.attr("isUsing","0");
										div.css({"background-color": "#FFFF66","cursor":"pointer"});
									}
									div.html(displayName);
									div.bind("click",function(){
										var lastClick = $('#gridID').val();
										if(lastClick){//获得上一次点击的ID，根据isUsing属性判断
											var d = $('#'+lastClick);
											var isUsing = d.attr("isUsing");
											if(isUsing=="1"){
												d.html(d.html().substring(0,d.html().length-6)+'(存有物品)');
												d.css({"background-color": "pink","cursor":"pointer"});
											}else{
												d.html(d.html().substring(0,d.html().length-6)+'(未存物品)');
												d.css({"background-color": "#FFFF66","cursor":"pointer"});
											}
										}
										$(this).html($(this).html().substring(0,$(this).html().length-6)+'(已经选中)');
										//设置点击后div的背景，以及页面隐藏域的值
										$(this).css({"background-color":"PaleTurquoise"});
										$('#gridID').val(this.id);
										
										if($(this).attr('isUsing')==1){
											$('#gridGoodsDiv').show();
											var gridGoodsDiv_data_body = $('#gridGoodsDiv_data_body');
											gridGoodsDiv_data_body.empty();
											for(var i=0;i<data[this.id].length;i++){
												var ntr = $('<tr>');
												var ntd1 = $('<td>');
												var ntd2 = $('<td>');
												var ntd3 = $('<td>');
												var ntd4 = $('<td>');
												ntd1.attr('width','100');
												ntd2.attr('width','100');
												ntd3.attr('width','100');
												ntd4.attr('width','100');
												ntd1.html(data[this.id][i].goods_name);
												ntd2.html(data[this.id][i].case_name);
												ntd3.html(data[this.id][i].goods_tcode);
												ntd4.html(data[this.id][i].create_date);
												ntd1.appendTo(ntr);
												ntd2.appendTo(ntr);
												ntd3.appendTo(ntr);
												ntd4.appendTo(ntr);
												ntr.appendTo(gridGoodsDiv_data_body);
											}
											var gridGoodsDiv_table = $('#gridGoodsDiv_table').show();
											$('#gridGoodsDiv').empty().append(gridGoodsDiv_table);
											gridGoodsDiv_table = $('#gridGoodsDiv_table').datagrid({height:$("#win_function").height()/2});
										}else{
											$('#gridGoodsDiv').hide();
										}
									});
									div.appendTo(td);
									td.appendTo(tr);
								}
								tr.appendTo(table);
							}
							while(shelfGoodslist.length>0){
								var shelfGoods = shelfGoodslist.shift();
								try{
									data[shelfGoods.case_grid_id].push(shelfGoods);
								}catch(e){
									
								}
							}
							tr.appendTo(table);
							table.appendTo(gridDiv);
						});
					}
				})
			}
		});
		var select_table_data_body = $('#select_table_data_body');
		select_table_data_body.empty();
		for(var i=0;i<objs.length;i++){
			$('#selectedGoodsIds').val($('#selectedGoodsIds').val()+objs[i].goods_id+',');
			var ntr = $('<tr>');
			var ntd1 = $('<td>');
			var ntd2 = $('<td>');
			var ntd3 = $('<td>');
			var ntd4 = $('<td>');
			ntd1.attr('width','100');
			ntd2.attr('width','100');
			ntd3.attr('width','100');
			ntd4.attr('width','100');
			ntd1.html(objs[i].goods_name);
			ntd2.html(objs[i].case_name);
			ntd3.html(objs[i].case_num);
			ntd4.html(objs[i].goods_id);
			ntd1.appendTo(ntr);
			ntd2.appendTo(ntr);
			ntd3.appendTo(ntr);
			ntd4.appendTo(ntr);
			ntr.appendTo(select_table_data_body);
		}
		//恶心,判断selectgrid是否有值，如果有值说明之前生成过表格，所以将生成的div层都删掉，将页面恢复成初始状态，并调用datagrid
		if(selectgrid){
			var select_table = $('#select_table').show();
			$('#select_goods_div').empty().append(select_table);
		}
		selectgrid = $('#select_table').datagrid({height:$("#win_function").height()/2});
		document.getElementById('t_code').focus();
	}
}
function doSave(){
	if($('#gridID').val().length==0){
		sys_alert('错误提示', '请选择格子再进行物品入库', 'error');
		return;
	}
	if($('#t_code').val().length==0){
		sys_alert('错误提示', '请输入条形码再进行物品入库', 'error');
		return;
	}
	form_func.form("submit",{
		url:proName+'/casePropertyStorage/doStorage',
		success:function(data){
			eval('data=' + data);
			if (data.success) {
				grid.datagrid('reload');
				win_func.window('close');
				greyLayer.close();
				objs = [];           //清空选择的入库物品
				grid.datagrid('clearSelections');  //清空表格选择，jqueryeasyUI恶心啊
			} else {
				sys_alert('错误提示', data.msg, 'error');
			}
		}
	});
}
function search(){
	var queryParams = $('#tt').datagrid('options').queryParams;
	queryParams["AJBH"] = $('#AJBH').val();
	queryParams["AJLB"] = $('#AJLB').val();
	queryParams["AJMC"] = $('#AJMC').val();
	queryParams["WPSBDW"] = $('#WPSBDW').val();
	queryParams["WPSBR"] = $('#WPSBR').val();
	queryParams["SBSJ"] = $('#SBSJ').val();
	$('#tt').datagrid('options').pageNumber = 1;
	$('#tt').datagrid('reload');
}
//js对象转json字符串
function obj2str(o){  
    var r = [];  
    if(typeof o =="string") return "\""+o.replace(/([\'\"\\])/g,"\\$1").replace(/(\n)/g," \\n").replace(/(\r)/g,"\\r").replace(/(\t)/g,"\\t")+"\"";  
    if(typeof o =="undefined") return "";  
    if(typeof o == "object"){  
        if(o===null) return "null";  
        else if(!o.sort){  
            for(var i in o){  
                r.push("\""+i+"\":"+obj2str(o[i]))  
            }  
            r="{"+r.join()+"}"  
        }else{  
            for(var i =0;i<o.length;i++)  
                r.push(obj2str(o[i]))  
            r="["+r.join()+"]"  
        }  
        return r;  
    }  
    return o.toString();  
} 
Array.prototype.removeGoods=function(obj){  
    for(var i =0;i <this.length;i++){  
        var temp = this[i];  
        if(!isNaN(obj)){  
            temp=i;  
        }  
        if(temp.goods_id == obj.goods_id){  
            for(var j = i;j <this.length;j++){  
                this[j]=this[j+1];  
            }  
            this.length = this.length-1;  
        }     
    }  
} 